package com.guigusuqi.user.service.impl;


import com.guigusuqi.user.entity.LoginUser;
import com.guigusuqi.user.entity.SysUser;
import com.guigusuqi.user.mapper.SysMenuMapper;
import com.guigusuqi.user.mapper.SysUserMapper;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service
public class UserDetailsServiceImpl implements UserDetailsService
{
    @Resource
    private SysUserMapper loginMapper;

    @Resource
    private SysMenuMapper sysMenuMapper;

    @Override
    public UserDetails loadUserByUsername(String userId) throws UsernameNotFoundException
    {
        SysUser sysUser = loginMapper.selectById(userId);

        if(Objects.isNull(sysUser))
        {
            throw new RuntimeException("手机号或者密码错误");
        }

        //  通过用户id查询权限集合
        List<String> permsList = sysMenuMapper.selectPermsByUserId(sysUser.getUserId());
        return new LoginUser(sysUser,permsList);//认证成功之后，把用户信息封装在LoginUser中
    }
}
